
ENT.Type = "anim"
ENT.Base = "base_gmodentity"

ENT.PrintName = "Repeater"
ENT.Author = "[GU]|ZLC|thomasfn"
ENT.Contact = "Don't."
ENT.Instructions = "Spawn it. Then link things to it to use it."
ENT.Purpose = ""

ENT.Spawnable = false
ENT.AdminSpawnable = false

ENT.ItemModel = "models/props_c17/utilityconnecter006c.mdl"
util.PrecacheModel( ENT.ItemModel )

ENT.IsRouter = true
ENT.IsRepeater = true

if (SERVER) then

	AddCSLuaFile( "shared.lua" )
	
	ENT.NextUse = CurTime()

	function ENT:Initialize()
		self.Entity:SetModel(self.ItemModel)
		self.Entity:PhysicsInit(SOLID_VPHYSICS)
		self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
		self.Entity:SetSolid(SOLID_VPHYSICS)
		
		if (!self.Data) then self.Data = {} end
		if (!self.Data[self:EntIndex()]) then self.Data[self:EntIndex()] = {} end
		
		local phys = self.Entity:GetPhysicsObject()
		if (phys && phys:IsValid()) then phys:Wake() end
	end

	function ENT:LookForIP( IP, filter, source )
		Msg( "Repeater: LookForIP Requested\n" )
		if (!self.Data[self:EntIndex()].Slots) then self.Data[self:EntIndex()].Slots = {} end
		local sl1 = self.Data[ self:EntIndex() ].Slots[ 1 ]
		local sl2 = self.Data[ self:EntIndex() ].Slots[ 2 ]
		local tar = nil
		if (source == sl1) then tar = sl2 end
		if (source == sl2) then tar = sl1 end
		if (!tar) then return false end
		Msg( "Repeater" .. tostring( self:EntIndex() ) .. ": LookForIP Forwarded to " .. tostring( tar:EntIndex() ) .. ".\n" )
		return tar:LookForIP( IP, filter, source )
	end
	
	function ENT:DeliverPacket( Packet, source )
		Msg( "Repeater" .. tostring( self:EntIndex() ) .. ": DeliverPacket Requested\n" )
		if (!self.Data[self:EntIndex()].Slots) then self.Data[self:EntIndex()].Slots = {} end
		local sl1 = self.Data[ self:EntIndex() ].Slots[ 1 ]
		local sl2 = self.Data[ self:EntIndex() ].Slots[ 2 ]
		local tar = nil
		if (source == sl1) then tar = sl2 end
		if (source == sl2) then tar = sl1 end
		if (!tar) then return false end
		Msg( "Repeater " .. tostring( self:EntIndex() ) .. ": DeliverPacket Forwarded to " .. tostring( tar:EntIndex() ) .. ".\n" )
		return tar:DeliverPacket( Packet, source )
	end
	
	function ENT:HasRouter( item )
		if (!self.Data[self:EntIndex()].Slots) then self.Data[self:EntIndex()].Slots = {} end
		local sl1 = self.Data[ self:EntIndex() ].Slots[ 1 ]
		local sl2 = self.Data[ self:EntIndex() ].Slots[ 2 ]
		return ((sl1 == item) || (sl2 == item))
	end
	
	function ENT:GetEmptySlot()
		if (!self.Data[self:EntIndex()].Slots) then self.Data[self:EntIndex()].Slots = {} end
		local sl = self.Data[self:EntIndex()].Slots
		if (sl[1]) then
			if (sl[2]) then
				return 0
			else
				return 2
			end
		else
			return 1
		end
		return 0
	end
	
	function ENT:FillSlot( slot, item )
		if (!self.Data[self:EntIndex()].Slots) then self.Data[self:EntIndex()].Slots = {} end
		self.Data[self:EntIndex()].Slots[ slot ] = item
	end
	
	function ENT:HasSpace()
		if (!self.Data[self:EntIndex()].Slots) then self.Data[self:EntIndex()].Slots = {} end
		return (#self.Data[self:EntIndex()].Slots<2)
	end
	
	function ENT:UpdateInt( var, value )
		self.Data[self:EntIndex()][var] = value
		self:SetNWInt( var, value )
	end
	
	function ENT:UpdateBool( var, value )
		self.Data[self:EntIndex()][var] = value
		self:SetNWBool( var, value )
	end
	
	function ENT:UpdateStr( var, value )
		self.Data[self:EntIndex()][var] = value
		self:SetNWString( var, value )
	end
	

end

if (CLIENT) then

	function ENT:Draw()
		// Draw our entity
		self.BaseClass.Draw( self )
	end	
end